<template>
  <div class="login">
    <div class="login-body">
      <el-card header="请先登录" class="login-card">
        <el-form @submit.native.prevent="login">
          <el-form-item label="用户名">
            <el-input type="text" v-model="model.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="model.password"></el-input>
          </el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {
        username: "admin",
        password: "123456"
      }
    };
  },
  methods: {
    async login() {
      const res = await this.$http.post("login", this.model);
      localStorage.token = res.data.token;
      localStorage.name = res.data.username;
      this.$router.push("/");
      this.$message({
        type: "success",
        message: "登录成功"
      });
    }
  }
};
</script>

<style>
.login-body{
  height: 100vh;
  display: flex;
  justify-self: center;
  align-items: center;
  background-size: cover;
  background-image: linear-gradient(to right , #7A88FF, #7AFFAF);
}
.login-card {
  width: 20rem;
  margin: auto;
}
</style>
